#include "FluidBB.h"

////////////////////////////////////////
// Building block
////////////////////////////////////////

int AddVelocity(const CKBehaviorContext& behcontext)
{
	CKBehavior* beh = behcontext.Behavior;
	CKContext *bc = behcontext.Context;
	Vx2DVector pos, dir;
	int size;

	beh->ActivateInput(0, FALSE);	// deactivation

	beh->GetInputParameterValue(0, &pos);
	beh->GetInputParameterValue(1, &dir);
	beh->GetInputParameterValue(2, &size);
	beh->GetInputParameterValue(3, &force);

	int i, j;
/*
	FOR_EACH_CELL
		u_prev[IX(i,j)] = v_prev[IX(i,j)] = 0.0f;
	END_FOR
*/
		i = (int)(pos.x * N);
	j = (int)(pos.y * N);

	for (int k = i - size; k < (i + size); k++)
	{
		if (k < 0)
			continue ;

		for (int l = j - size; l < (j + size); l++)
		{
			if (l < 0)
				continue ;

			u_prev[IX(k,l)] = dir.x * force;
			v_prev[IX(k,l)] = dir.y * force;
		}
	}

	
	//dens_prev[IX(N/2,N/2)] = source;
	
	beh->ActivateOutput(0);
	return (CKBR_OK);
}

CKERROR CreateAddVelocityProto ( CKBehaviorPrototype **pproto)
{
	CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("AddVelocity");

	if(!proto)
		return (CKERR_OUTOFMEMORY);

	proto->DeclareInput("In");
	proto->DeclareOutput("Out");
	
	proto->DeclareInParameter("Position", CKPGUID_2DVECTOR);
	proto->DeclareInParameter("Direction", CKPGUID_2DVECTOR);
	proto->DeclareInParameter("Size", CKPGUID_INT, "1");
	proto->DeclareInParameter("Force", CKPGUID_FLOAT, "5.0");
	
	proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
	proto->SetFunction(AddVelocity);
	//proto->SetBehaviorFlags(CKBEHAVIOR_TARGETABLE);

	*pproto = proto;
	return (CK_OK);
}

CKObjectDeclaration *FillBehaviorAddVelocity()
{
	CKObjectDeclaration *od = CreateCKObjectDeclaration ( "AddVelocity" );
	od->SetDescription ( "Adds force in the velocity field of the fluid." );
	od->SetCategory ( "Fluids" );
	od->SetType ( CKDLL_BEHAVIORPROTOTYPE );
	od->SetGuid ( ADDVELOCITY_GUID );
	od->SetAuthorGuid ( AUTHOR_GUID );
	od->SetAuthorName ( "Jonathan DERROUGH" );
	od->SetVersion ( 0x00000001 );
	od->SetCreationFunction ( CreateAddVelocityProto );
	//od->SetCompatibleClassId ( CKCID_3DENTITY );
	return od;
}
